package com.qububu.common.position.control;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qububu.common.position.dto.PositionInfoDto;
import com.qububu.common.position.service.PositionService;
import static com.qububu.utils.ParamConverter.assertNotEmpty;
import com.qububu.utils.SystemConstants;
import com.qububu.utils.SystemUtil;

/**
 * 小区的相关操作
 * */
@Controller
@RequestMapping("/position")
public class PositionControl {

	@Resource
	private PositionService positionService;
	
	@RequestMapping("/admin/adminPosition")
	public String adminPosition() {
		
		return "adminPosition";
	}
	
	/**
	 * 添加小区
	 * */
	@RequestMapping("/admin/addPosition")
	@ResponseBody
	public Map<String, Object> addPosition(HttpServletRequest req){
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		PositionInfoDto position = createPositionInfoDtoEntity(req);
		
		if (positionService.doAddPosition(position)) {
			
			resultMap.put("result", SystemConstants.AJAX_SUCCESS);
		} else {
			resultMap.put("result", SystemConstants.AJAX_FAILED_ALERT);
		}
		
		return resultMap;
	}
	
	private PositionInfoDto createPositionInfoDtoEntity(HttpServletRequest req) {
		
		String positionId = SystemUtil.getUUIDString();
		PositionInfoDto position = new PositionInfoDto(positionId);
		
		String positionName = assertNotEmpty(req.getParameter("positionName"));
		position.setPositionName(positionName);
		
		return position;
	}
	
	/**
	 * 查询地址列表
	 * */
	@RequestMapping("/getPositions")
	@ResponseBody
	public Map<String, Object> getPositionList() {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		
		List<PositionInfoDto> positionList = positionService.getPositionList();
		
		resultMap.put("result", SystemConstants.AJAX_SUCCESS);
		resultMap.put("positions", positionList);
		
		return resultMap;
	}
	
	/**
	 * 修改地址信息
	 * */
	@RequestMapping("/admin/modifyPosition")
	@ResponseBody
	public Map<String, Object> modifyPosition(HttpServletRequest req){
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String positionId = assertNotEmpty(req.getParameter("positionId"));
		String positionName = assertNotEmpty(req.getParameter("positionName"));
		
		if (positionService.doModifyPosition(positionId, positionName)) {
			
			resultMap.put("result", SystemConstants.AJAX_SUCCESS);
		} else {
			resultMap.put("result", SystemConstants.AJAX_FAILED_ALERT);
		}
		
		return resultMap;
	}
	
	/**
	 * 删除地址信息
	 * */
	@RequestMapping("/admin/deletePosition")
	@ResponseBody
	public Map<String, Object> deletePosition(HttpServletRequest req){
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String positionId = assertNotEmpty(req.getParameter("positionId"));
		
		if (positionService.doDeletePosition(positionId)) {
			
			resultMap.put("result", SystemConstants.AJAX_SUCCESS);
		} else {
			resultMap.put("result", SystemConstants.AJAX_FAILED_ALERT);
		}
		
		return resultMap;
	}
}
